﻿using System;
namespace Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="omandType", Namespace="http://producers.arireg.xtee.riik.ee/producer/arireg", Target=typeof(OmandType))]
    public class OmandTypeSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IOmandType clazz;
        
        public OmandTypeSerializer(IOmandType clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("suurus");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "decimal");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdDecimal(((decimal)(clazz.Suurus))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("omandiliik");
            if (clazz.Omandiliik!=null) {
                Writer.WriteTypeAttribute("http://producers.arireg.xtee.riik.ee/producer/arireg", "x_omandiliik");
                Writer.WriteString(clazz.Omandiliik.ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Omandiliik");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("roll");
            if (clazz.Roll!=null) {
                Writer.WriteTypeAttribute("http://producers.arireg.xtee.riik.ee/producer/arireg", "x_isikuroll");
                Writer.WriteString(clazz.Roll.ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Roll");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("omanikud");
            if (clazz.Omanikud!=null) {
                Writer.WriteTypeAttribute("http://schemas.xmlsoap.org/soap/encoding/", "Array");
                Writer.WriteArrayTypeAttribute("kapitaliOmanikType", "http://producers.arireg.xtee.riik.ee/producer/arireg", clazz.Omanikud.Count);
                if (((clazz.Omanikud == null) 
                            || (clazz.Omanikud.Count < 1m))) {
                    throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                }
                for (int x = 0; ((clazz.Omanikud != null) 
                            && (x < clazz.Omanikud.Count)); x = (x + 1)) {
                    Writer.WriteStartElement("item");
                    if (clazz.Omanikud[x]!=null) {
                        global::Xtee.Core.SerializationMetadata metaOmanikud = this.GetMetaData(clazz.Omanikud[x]);
                        Writer.WriteTypeAttribute(metaOmanikud.TypeQualifiedName.Namespace, metaOmanikud.TypeQualifiedName.Name);
                        metaOmanikud.NewSerializer(clazz.Omanikud[x]).WriteXml(Writer);
                    }
                    else {
                        Writer.WriteAttributeString("xsi", "nil", null, "1");
                    }
                    Writer.WriteEndElement();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Omanikud");
            }
            Writer.WriteEndElement();
            if (clazz.Liikmesus!=null) {
                Writer.WriteStartElement("liikmesus");
                global::Xtee.Core.SerializationMetadata metaLiikmesus = this.GetMetaData(clazz.Liikmesus);
                Writer.WriteTypeAttribute(metaLiikmesus.TypeQualifiedName.Namespace, metaLiikmesus.TypeQualifiedName.Name);
                metaLiikmesus.NewSerializer(clazz.Liikmesus).WriteXml(Writer);
                Writer.WriteEndElement();
            }
            if (clazz.Kontakt!=null) {
                Writer.WriteStartElement("kontakt");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Kontakt))).ToString());
                Writer.WriteEndElement();
            }
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="suurus") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>omandType>suurus");
                }
                clazz.Suurus = new global::Xtee.Core.Schema.XsdDecimal(Reader.ReadElementString("suurus")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>omandType>suurus", Reader.LocalName);
            }
            if (Reader.LocalName=="omandiliik") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>omandType>omandiliik");
                }
                clazz.Omandiliik = new global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.XOmandiliik(Reader.ReadElementString("omandiliik"));
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>omandType>omandiliik", Reader.LocalName);
            }
            if (Reader.LocalName=="roll") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>omandType>roll");
                }
                clazz.Roll = new global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.XIsikuroll(Reader.ReadElementString("roll"));
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>omandType>roll", Reader.LocalName);
            }
            if (Reader.LocalName=="omanikud") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>omandType>omanikud");
                }
                clazz.Omanikud = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKapitaliOmanikType>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("omanikud");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if (Reader.LocalName=="item") {
                            if ((Reader.IsElementNil() == false)) {
                                global::Xtee.Core.SerializationMetadata metaOmanikud = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKapitaliOmanikType>(Reader, "kapitaliOmanikType", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                                global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKapitaliOmanikType item = metaOmanikud.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKapitaliOmanikType>();
                                clazz.Omanikud.Add(item);
                                if ((Reader.IsEmptyElement == false)) {
                                    Reader.ReadStartElement("item");
                                    metaOmanikud.NewSerializer(item).ReadXml(Reader);
                                    Reader.ReadEndElement();
                                }
                                else {
                                    Reader.Skip();
                                }
                            }
                            else {
                                clazz.Omanikud.Add(null);
                                Reader.Skip();
                            }
                        }
                        else {
                            throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">item", Reader.LocalName);
                        }
                    }
                    if ((clazz.Omanikud.Count < 1m)) {
                        throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>omandType>omanikud", Reader.LocalName);
            }
            if (Reader.LocalName=="liikmesus") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>omandType>liikmesus");
                }
                global::Xtee.Core.SerializationMetadata metaLiikmesus = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IHyLiikmesusType>(Reader, "hy_liikmesusType", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                clazz.Liikmesus = metaLiikmesus.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IHyLiikmesusType>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("liikmesus");
                    metaLiikmesus.NewSerializer(clazz.Liikmesus).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            if (Reader.LocalName=="kontakt") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>omandType>kontakt");
                }
                clazz.Kontakt = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("kontakt")).Value;
            }
        }
    }
}
